﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Helpers;
using System.Linq;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using WPFExcelReport.Controls;
using WPFExcelReport; 
namespace INTEX.SCHEDULER
{

    public partial class ITXSSPA
    {
        VICC_MKRF _VMK;
        public VICC_MKRF VMK
        {
            get
            {
                if(_VMK == null) _VMK = new VICC_MKRF();
                return _VMK;
            }
        }
        DataTable tb;
       
        public void ShowMachine()
        {

            //SetProd.Clear();
            //SetProd.Merge(DBSchedulePrepare(SetData));
            foreach (DataRow row in TBAM.Rows)
            {
                ShowMachineID(row["MACHINEID"].ToString());               
            }
        }
        public void ShowMachineID(string MachineID)
        {
            //if (MachineID != "mM_0002_00")
            //continue;
            UIAM Machine = null;
            Machine = (UIAM)MachinesLib[MachineID];
            if (Machine == null)
                return;
            filter = "MACHINEID='" + MachineID + "'";
            sort = "MKFPLDATV ASC,MKFPLTIME ASC";
            rows = BASICJOBS.Select(filter, sort);
            if (rows.Length == 0)
                return;
            Machine.JOBS = rows.CopyToDataTable();
            Machine.ShowJobs();
        }
       
        public void DrawCalendar()
        { 
            
            if (ZoneCalendar != null)
            { 
                Task.Factory.StartNew(() => UpdateStack(MCALENDAR, ZoneCalendar), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
            }
         }
        public void ReSetDensity()
        {
           
            foreach (KeyValuePair<string, UIAM> kp in MachinesLib)
            {
                UIAM m = kp.Value;
                m.MGANTT.ReSetGrid();
            }
        }
        public void DrawMachine(bool increase = true)
        {

            int even = 0;
            string filter = "AMMANDANT='00'";

            string sort = "AMNRKRS ASC,AMLFDNR ASC,AMPOS ASC";
            if (!increase)
            {
                sort = "AMNRKRS DESC,AMLFDNR DESC,AMPOS DESC";
            }
            DataRow[] rows = TBAM.Select(filter, sort);
            foreach (DataRow dwMachine in rows)
            {
                DrawOneMachie(dwMachine, even);
                even++;
                if (even > 1)
                    even = 0;
            }
        }
        public void DrawOneMachie(DataRow dwMachine,int even)
        {
            
            UIAM m;
            string MACHINEID = dwMachine["MACHINEID"].ToString();// Helper.GetMachineID(row["AMNRKRS"].ToString(), row["AMLFDNR"].ToString(), row["AMPOS"].ToString());
            string AMBEZ = dwMachine["AMBEZ"].ToString().Trim();
            string key = MACHINEID;
            Type docType;
            switch (dwMachine["AMART"].ToString().Trim())
            {
                case "RSGYG":
                    docType = typeof(YDMachine);
                    break;
                case "XS":
                    docType = typeof(SpinMachine);
                    break;
                case "99999999":
                    docType = typeof(SpinPlanMachine);
                    break;
                case "WEBMA":
                    docType = typeof(LOOM);
                    break;
                case "DX":
                case "SM":
                case "SG":
                case "DXJ":
                case "SMJ":
                case "SGJ":
                    docType = typeof(TDXJ);
                    break;
                default:
                    docType = typeof(SpinMachine);
                    break;
            }
            if (MachinesLib.ContainsKey(key))
            {
                m = (UIAM)MachinesLib[key];

            }
            else
            {
                m = (UIAM)Activator.CreateInstance(docType);
                m.IsMonitoring = false;
                MachinesLib.Add(key, m);
            }
            m.JobsLib = this.JobsLib;
            m.SPSPIN = TBSPSPIN;
            m.dwMachine = dwMachine;
            m.MCALENDAR = MCALENDAR;
            if (even == 0)
            {
                m.SetUp(true);
            }
            else
            {
                m.SetUp(false);
            } 
           
            if (MachinePanel != null)
            {
                UpdateStack(m, MachinePanel);
                UpdateStack(m.MGANTT, GanttPanel);
            }
        }
        public void UpdateCalendar()
        {
            UIAM m;
            foreach (KeyValuePair<string, UIAM> kp in MachinesLib)
            {
                m = kp.Value;
                m.MCALENDAR = MCALENDAR;
                m.PrepareMGANTT(m.IsEven);
                int count = m.MGANTT.MainGrid.Children.Count;
                if (count > 1)
                {
                    m.MGANTT.MainGrid.Children.RemoveRange(1, count - 1);
                }
                m.ShowJobs();
            }
        }
        public void UpdateStack(object obj, StackPanel st=null)
        {
            if (st == null)
                return;
            string name = "";
            if (obj.GetType() == typeof(Grid))
            {
                Grid gd = (Grid)obj;
                name = gd.Tag as string;
                if (Pagescope.ContainsKey(name))
                {
                    Pagescope.UnregisterName(name);
                }
                if (st.Children.Contains(gd))
                {
                    st.Children.Remove(gd);
                }
                st.Children.Add(gd);
                Pagescope.RegisterName(name, gd);
            }
            if (obj.GetType().BaseType == typeof(UIAM))
            {
                UIAM gd = (UIAM)obj;
                name = gd.Name;
                if (Pagescope.ContainsKey(name))
                {
                    Pagescope.UnregisterName(name);
                }
                if (st.Children.Contains(gd))
                {
                    st.Children.Remove(gd);
                }
                st.Children.Add(gd);
                Pagescope.RegisterName(name, gd);
            }
            if (obj.GetType() == typeof(MCALENDAR))
            {
                MCALENDAR gd = (MCALENDAR)obj;
                name = gd.Name;
                if (Pagescope.ContainsKey(name))
                {
                    Pagescope.UnregisterName(name);
                }
                if (st.Children.Contains(gd))
                {
                    st.Children.Remove(gd);
                }
                st.Children.Add(gd);
                Pagescope.RegisterName(name, gd);
            }
            if (obj.GetType() == typeof(MGANTT))
            {
                MGANTT gd = (MGANTT)obj;
                name = gd.Name;
                if (Pagescope.ContainsKey(name))
                {
                    Pagescope.UnregisterName(name);
                }
                if (st.Children.Contains(gd))
                {
                    st.Children.Remove(gd);
                }
                st.Children.Add(gd);
                Pagescope.RegisterName(name, gd);
            }

        }

        public void UpdateMGANTT(UIAM m, bool IsVisible = false)
        {
            if (IsVisible)
            {
                m.IsShow =true;
                
            }
            else
            {
                m.IsShow = false;
            }
        }

        public void FilterByMachineType(string SMTYP)
        {
            //dwBMKRF

            UIAM m;
            foreach (KeyValuePair<string, UIAM> kp in MachinesLib)
            {
                m = kp.Value;
                if (SMTYP == "")
                {
                    if (m.IsShow)
                        continue;
                    
                    Task.Factory.StartNew(() => UpdateMGANTT(m, true), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
                    continue;
                }
                if (m.SMTYP == SMTYP)
                {
                    if (m.IsShow)
                        continue;
                    
                     Task.Factory.StartNew(() => UpdateMGANTT(m, true), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
                }
                else
                {
                    if (m.IsShow)
                    {
                        Task.Factory.StartNew(() => UpdateMGANTT(m, false), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
                    }
                }
            }
        }
        public void PrepareAllJob(bool IsReset)
        {

            VMK.PrepareAllJob(Pagescope, BASICJOBS, TBJOBSHOW, IsReset, JobsLib, MachinesLib, MCALENDAR);
           
        }
    }
}

